package com.yqritc.scalablevideoview.sample;

import com.yqritc.scalablevideoview.ScalableVideoView;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PointF;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.io.IOException;


@SuppressLint("NewApi") 
public class MainActivity extends Activity implements View.OnClickListener {

    private ScalableVideoView mVideoView;
    private LinearLayout mLayout;
    
    /** 屏幕宽度 */
    private int screenWidth;
    /** 屏幕高度 */
	private int screenHeight;

	/** videoview的外层layout高度 */
	private int layoutHeight = 400;
	
	/** 拖拽状态 */
	private static int DRAG = 1;
	/** 缩放状态 */
	private static int ZOOM = 2;
	/** 无状态 */
	private static int NONE = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
		mLayout = (LinearLayout)findViewById(R.id.layout_videoview);
        mVideoView = (ScalableVideoView) findViewById(R.id.video_view);
        findViewById(R.id.btn_next).setOnClickListener(this);
        findViewById(R.id.btn_get).setOnClickListener(this);
        
        layoutHeight = mLayout.getHeight();
        WindowManager wm = this.getWindowManager();
		screenWidth = wm.getDefaultDisplay().getWidth();
		screenHeight = wm.getDefaultDisplay().getHeight();

        try {
//            mVideoView.setRawData(R.raw.landscape_sample);     //本地视频
            mVideoView.setDataUri("http://v.cctv.com/flash/mp4video28/TMS/2013/05/06/265114d5f2e641278098503f1676d017_h264418000nero_aac32-1.mp4");  //在线视频
            mVideoView.setVolume(0, 0);
            mVideoView.setLooping(true);
            mVideoView.prepare(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mVideoView.start();
                }
            });
        } catch (IOException ioe) {
            //ignore
        }
        
        mVideoView.setOnTouchListener(movingEventListener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
            	Animator anim = AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set_scale);  
				mVideoView.setPivotX(360);  
		        mVideoView.setPivotY(200);  
		        //显示的调用invalidate  
		        anim.setTarget(mVideoView);  
		        anim.start();  
                break;
            case R.id.btn_get:
            	 int left = mVideoView.getLeft();
				 int top = mVideoView.getTop();
				 int right = mVideoView.getRight();
				 int bottom = mVideoView.getBottom();
				 int width = mVideoView.getWidth();
				 int height = mVideoView.getHeight();
				 
				 Log.d("zr", "left = " + left);
				 Log.d("zr", "top = " + top);
				 Log.d("zr", "right = " + right);
				 Log.d("zr", "bottom = " + bottom);
				 Log.d("zr", "width = " + width);
				 Log.d("zr", "height = " + height);
            	break;
            default:
                break;
        }
    }
    
    
    public OnTouchListener movingEventListener = new OnTouchListener() {
		int lastX, lastY;
		int mode;
		float oldDist;
		//上一次的缩放比例
		float preScale;
		

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				lastX = (int) event.getRawX();
				lastY = (int) event.getRawY();
				mode = DRAG;
				Log.d("zr", "mode = " + mode);
				break;
			case MotionEvent.ACTION_POINTER_DOWN:
				oldDist = spacing(event);
				if (oldDist > 10f) {
					mode = ZOOM;
					Log.d("zr", "mode = " + mode);
				}
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_POINTER_UP:
				if (mode == ZOOM) {
					float newDist = spacing(event);
					if (newDist > 10f) {
						float scale = newDist/oldDist;
						float x = event.getX(0) - event.getX(1);
						float y = event.getY(0) - event.getY(1);
						
						int width = mVideoView.getWidth();
						int height = mVideoView.getHeight();
						
						int scaleWidth = (int) (mVideoView.getWidth() * scale);
						int scaleheight = (int) (mVideoView.getHeight() * scale);
						if(scaleWidth < screenWidth){
							scale = 1;
							PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", preScale, scale);
							PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", preScale, scale);
							ObjectAnimator.ofPropertyValuesHolder(v, pvhX, pvhY).setDuration(1000).start();
						}else{
							PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", preScale, scale);
							PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", preScale, scale);
							ObjectAnimator.ofPropertyValuesHolder(v, pvhX, pvhY).setDuration(1000).start();
						}
				        preScale = scale;
				        
					}
				}
				
				mode = NONE;
				break;
			case MotionEvent.ACTION_MOVE:
				if (mode == DRAG) {
					 int dx = (int) event.getRawX() - lastX;
					 int dy = (int) event.getRawY() - lastY;
					
					 int left = v.getLeft() + dx;
					 int top = v.getTop() + dy;
					 int right = v.getRight() + dx;
					 int bottom = v.getBottom() + dy;
					 // 设置不能小于容器
//					 if (left > 0) {
//						 left = 0;
//						 right = left + v.getWidth();
//					 }
//					
//					 if (right < screenWidth) {
//						 right = screenWidth;
//						 left = right - v.getWidth();
//					 }
//					
//					 if (top > 0) {
//						 top = 0;
//						 bottom = top + v.getHeight();
//					 }
//					
//					 if (bottom < layoutHeight) {
//						 bottom = layoutHeight;
//						 top = bottom - v.getHeight();
//					 }
					
					 v.layout(left, top, right, bottom);
					 Log.d("zr", "left = " + left);
					 Log.d("zr", "top = " + top);
					 Log.d("zr", "right = " + right);
					 Log.d("zr", "bottom = " + bottom);
					
					 lastX = (int) event.getRawX();
					 lastY = (int) event.getRawY();
					
				} 
				break;
			}

			return true; 
		}

		private void dumpEvent(MotionEvent event) {
			String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
					"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
			StringBuilder sb = new StringBuilder();
			int action = event.getAction();
			int actionCode = action & MotionEvent.ACTION_MASK;
			sb.append("event ACTION_").append(names[actionCode]);
			if (actionCode == MotionEvent.ACTION_POINTER_DOWN
					|| actionCode == MotionEvent.ACTION_POINTER_UP) {
				sb.append("(pid ").append(
						action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
				sb.append(")");
			}
			sb.append("[");
			for (int i = 0; i < event.getPointerCount(); i++) {
				sb.append("#").append(i);
				sb.append("(pid ").append(event.getPointerId(i));
				sb.append(")=").append((int) event.getX(i));
				sb.append(",").append((int) event.getY(i));
				if (i + 1 < event.getPointerCount())
					sb.append(";");
			}
			sb.append("]");
		}
		
		private float spacing(MotionEvent event) {
			float x = event.getX(0) - event.getX(1);
			float y = event.getY(0) - event.getY(1);
			return FloatMath.sqrt(x * x + y * y);
		}

		private void midPoint(PointF point, MotionEvent event) {
			float x = event.getX(0) + event.getX(1);
			float y = event.getY(0) + event.getY(1);
			point.set(x / 2, y / 2);
		}
	};
}
